#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    this->player = new QMediaPlayer(this);
    this->player->setNotifyInterval(2000);

    QGraphicsScene * scene = new QGraphicsScene(this);
    ui->graphicsView->setScene(scene);

    this->videoItem = new QGraphicsVideoItem;
    this->videoItem->setSize(QSizeF(ui->graphicsView->width(), ui->graphicsView->height()));

    this->videoItem->setFlags(QGraphicsItem::ItemIsMovable|QGraphicsItem::ItemIsSelectable|QGraphicsItem::ItemIsFocusable);


    scene->addItem(videoItem);
    this->player->setVideoOutput(videoItem);

    connect(this->player,
            SIGNAL(stateChanged(QMediaPlayer::State)),
            this,
            SLOT(onStateChanged(QMediaPlayer::State))
            );
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_btn_open_clicked()
{
    QString currentPath = QDir::homePath();
    QString openFileDialogTitle = "请选择视频文件(vico)";
    QString fileFilter = "mp4文件(*.mp4);;所有文件(*.*)";

    QString filePath = QFileDialog::getOpenFileName(this, openFileDialogTitle, currentPath, fileFilter);

    if(filePath.isEmpty()){
        QMessageBox::warning(this, "警告", "请选择视频文件!");
        return;
    }

    QFileInfo fileInfo(filePath);
    ui->label_fileName->setText(fileInfo.fileName());



    this->player->setMedia(QUrl::fromLocalFile(filePath));

    //this->player->play();
}

void MainWindow::onStateChanged(QMediaPlayer::State stat)
{
    ui->btn_play->setEnabled(!(stat == QMediaPlayer::PlayingState));
    ui->btn_suspend->setEnabled(!(stat == QMediaPlayer::PlayingState));
    ui->btn_stop->setEnabled(!(stat == QMediaPlayer::PlayingState));
}

void MainWindow::onDurationChanged(qint64 duration)
{

}

void MainWindow::onPositionChanged(qint64 position)
{

}

void MainWindow::on_btn_play_clicked()
{
    this->player->play();
}

void MainWindow::on_btn_turn_of_sound_clicked()
{
    bool mte = this->player->isMuted();
    this->player->setMuted(!mte);

    if(mte){
        ui->btn_turn_of_sound->setText("有声音");
    }else{
        ui->btn_turn_of_sound->setText("静音");
    }
}
